{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cfdf4f09-8125-4ed1-8063-6feed57da8a3",
   "metadata": {},
   "source": [
    "# How to init any model in one line\n",
    "\n",
    "Many LLM applications let end users specify what model provider and model they want the application to be powered by. This requires writing some logic to initialize different ChatModels based on some user configuration. The `init_chat_model()` helper method makes it easy to initialize a number of different model integrations without having to worry about import paths and class names.\n",
    "\n",
    ":::tip Supported models\n",
    "\n",
    "See the [init_chat_model()](https://api.python.langchain.com/en/latest/chat_models/langchain.chat_models.base.init_chat_model.html) API reference for a full list of supported integrations.\n",
    "\n",
    "Make sure you have the integration packages installed for any model providers you want to support. E.g. you should have `langchain-openai` installed to init an OpenAI model.\n",
    "\n",
    ":::"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "165b0de6-9ae3-4e3d-aa98-4fc8a97c4a06",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -qU langchain langchain-openai langchain-anthropic langchain-google-vertexai"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea2c9f57-a796-45f8-b6f4-3efd3f361a9b",
   "metadata": {},
   "source": [
    "## Basic usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "79e14913-803c-4382-9009-5c6af3d75d35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GPT-4o: I'm an AI created by OpenAI, and I don't have a personal name. You can call me Assistant! How can I help you today?\n",
      "\n",
      "Claude Opus: My name is Claude. It's nice to meet you!\n",
      "\n",
      "Gemini 1.5: I am a large language model, trained by Google. I do not have a name. \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from langchain.chat_models import init_chat_model\n",
    "\n",
    "# Returns a langchain_openai.ChatOpenAI instance.\n",
    "gpt_4o = init_chat_model(\"gpt-4o\", model_provider=\"openai\", temperature=0)\n",
    "# Returns a langchain_anthropic.ChatAnthropic instance.\n",
    "claude_opus = init_chat_model(\n",
    "    \"claude-3-opus-20240229\", model_provider=\"anthropic\", temperature=0\n",
    ")\n",
    "# Returns a langchain_google_vertexai.ChatVertexAI instance.\n",
    "gemini_15 = init_chat_model(\n",
    "    \"gemini-1.5-pro\", model_provider=\"google_vertexai\", temperature=0\n",
    ")\n",
    "\n",
    "# Since all model integrations implement the ChatModel interface, you can use them in the same way.\n",
    "print(\"GPT-4o: \" + gpt_4o.invoke(\"what's your name\").content + \"\\n\")\n",
    "print(\"Claude Opus: \" + claude_opus.invoke(\"what's your name\").content + \"\\n\")\n",
    "print(\"Gemini 1.5: \" + gemini_15.invoke(\"what's your name\").content + \"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fff9a4c8-b6ee-4a1a-8d3d-0ecaa312d4ed",
   "metadata": {},
   "source": [
    "## Simple config example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75c25d39-bf47-4b51-a6c6-64d9c572bfd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_config = {\n",
    "    \"model\": \"...user-specified...\",\n",
    "    \"model_provider\": \"...user-specified...\",\n",
    "    \"temperature\": 0,\n",
    "    \"max_tokens\": 1000,\n",
    "}\n",
    "\n",
    "llm = init_chat_model(**user_config)\n",
    "llm.invoke(\"what's your name\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f811f219-5e78-4b62-b495-915d52a22532",
   "metadata": {},
   "source": [
    "## Inferring model provider\n",
    "\n",
    "For common and distinct model names `init_chat_model()` will attempt to infer the model provider. See the [API reference](https://api.python.langchain.com/en/latest/chat_models/langchain.chat_models.base.init_chat_model.html) for a full list of inference behavior. E.g. any model that starts with `gpt-3...` or `gpt-4...` will be inferred as using model provider `openai`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0378ccc6-95bc-4d50-be50-fccc193f0a71",
   "metadata": {},
   "outputs": [],
   "source": [
    "gpt_4o = init_chat_model(\"gpt-4o\", temperature=0)\n",
    "claude_opus = init_chat_model(\"claude-3-opus-20240229\", temperature=0)\n",
    "gemini_15 = init_chat_model(\"gemini-1.5-pro\", temperature=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da07b5c0-d2e6-42e4-bfcd-2efcfaae6221",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "poetry-venv-2",
   "language": "python",
   "name": "poetry-venv-2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
